from django.forms import Widget


# from django.forms.widgets import FileInput, HiddenInput


class UpLoadFIleWidget(Widget):
    template_name = 'widgets/element_file_upload.html'

    def get_context(self, name, value, attrs):
        context = {}
        context['widget'] = {
            'file_list': [{'name': name.split('/')[-1], 'url': value}] if value else [],
            'name': name,
            'is_hidden': self.is_hidden,
            'required': self.is_required,
            'value': self.format_value(value),
            'attrs': self.build_attrs(self.attrs, attrs),
            'template_name': self.template_name,
        }
        return context


# 图片上传外键版
class ImageUploadFKWidget(Widget):
    template_name = 'widgets/fk_element_file_upload.html'

    def __init__(self, attrs=None):
        super().__init__(attrs)

    # self.attrs = {} if attrs is None else attrs.copy()

    def get_context(self, name, value, attrs):
        context = {'file_list': [], 'pk': ''}

        if value:
            obj = self.choices.queryset.filter(pk=value).first()
            if obj:
                context['file_list'].append({'name': obj.name.split('/')[-1], 'url': obj.get_url()})
                context['pk'] = obj.pk
        context['widget'] = {
            'name': name,
            'is_hidden': self.is_hidden,
            'required': self.is_required,
            'value': self.format_value(value),
            'attrs': self.build_attrs(self.attrs, attrs),
            'template_name': self.template_name,
        }
        return context


class ImageHiddenWidget(Widget):
    template_name = 'widgets/fk_element_file_upload.html'

    def __init__(self, attrs=None):
        super().__init__(attrs)

    # self.attrs = {} if attrs is None else attrs.copy()

    def get_context(self, name, value, attrs):
        context = {'file_list': [], 'pk': ''}

        if value:
            obj = self.choices.queryset.filter(pk=value).first()
            if obj:
                context['file_list'].append({'name': obj.name.split('/')[-1], 'url': obj.get_url()})
                context['pk'] = obj.pk
        context['widget'] = {
            'name': name,
            'is_hidden': self.is_hidden,
            'required': self.is_required,
            'value': self.format_value(value),
            'attrs': self.build_attrs(self.attrs, attrs),
            'template_name': self.template_name,
        }
        return context


class TimeRangWidget(Widget):
    template_name = 'widgets/eTimeRang.html'

    def __init__(self, attrs=None):
        super().__init__(attrs)

    def get_context(self, name, value, attrs):
        context = {}
        if value:
            value_list = [i.split(':') for i in value.split(' - ')]
        else:
            value_list = []
        context['options'] = []
        context['widget'] = {
            'name': name,
            'is_hidden': self.is_hidden,
            'required': self.is_required,
            'value': self.format_value(value),
            'value_list': value_list,
            'attrs': self.build_attrs(self.attrs, attrs),
            'template_name': self.template_name,
        }
        return context

    # def get_context(self, name, value, attrs):
    #     context = {'file_list': [], 'pk': ''}
    #
    #     if value:
    #         obj = self.choices.queryset.filter(pk=value).first()
    #         if obj:
    #             context['file_list'].append({'name': obj.name.split('/')[-1], 'url': obj.get_url()})
    #             context['pk'] = obj.pk
    #     context['widget'] = {
    #         'name': name,
    #         'is_hidden': self.is_hidden,
    #         'required': self.is_required,
    #         'value': self.format_value(value),
    #         'attrs': self.build_attrs(self.attrs, attrs),
    #         'template_name': self.template_name,
    #     }
    #     return context
